﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Xml;
using Commom;
using Common;
using Common.WebApi;
using FScheduler;
using Model.Database;
using Model.DbContext;
using QScheduler;

namespace ConsoleTest
{
    class Program
    {
        //[Serializable()]
        //class Test
        //{
        //    public int Id { get; set; }
        //}

        static void Main(string[] args)
        {
            #region WebCache

            //更新测试--成功
            //WebCacheHelper.AddCache("key", new Test() { Id = 666 });
            //for (int i = 0; i < 100; i++)
            //{
            //    Cache che = WebCacheHelper.GetCache("key");
            //    WebCacheHelper.UpdateCache("key", new Test() { Id = i });
            //}

            #endregion

            #region MemoryCache

            //更新测试--成功
            //MemoryCacheHelper.AddCache("key", new Test() { Id = 666 });
            //for (int i = 0; i < 100; i++)
            //{
            //    Cache cache = MemoryCacheHelper.GetCache("key");
            //    MemoryCacheHelper.UpdateCache("key", new Test() { Id = i });
            //}

            //删除测试--成功
            //for (int i = 0; i < 100; i++)
            //{
            //    MemoryCacheHelper.AddCache("key" + i, new Test() { Id = 666 });
            //}
            //for (int i = 0; i < 100; i++)
            //{
            //    MemoryCacheHelper.RemoveCache("key" + i);
            //}

            #endregion

            #region Quartz.Net

            //测试成功
            //QuartzBll.Start();

            #endregion

            #region FluentScheduler

            //测试成功
            //FluentBll.Start();

            #endregion

            #region 发邮件

            //EMailHelper eMailHelper=new EMailHelper();
            //eMailHelper.EMailName = ConfigHelper.GetConfigString("MailName");
            //eMailHelper.EMailServer = ConfigHelper.GetConfigString("MailHost");
            //eMailHelper.EMailUserName = ConfigHelper.GetConfigString("MailUserName");
            //eMailHelper.EMailPassword = ConfigHelper.GetConfigString("MailPassword");
            //eMailHelper.Send("728607814@qq.com", "测试", "童小鱼测试",true,true);
            //成功
            #endregion

            #region 缩略图

            //ImageHelper.MakeThumbnailImage(@"C:\Users\admin\Desktop\2.png", @"C:\Users\admin\Desktop\2SL.png", 50,50, "HW");
            //成功
            #endregion

            #region EF写法Demo

            //ModelContainer modelContainer=new ModelContainer();

            ////左连接（保留左表）
            //var linq=  from A in modelContainer.DataDictionaryHeads
            //           join B in modelContainer.DataDictionaryItems
            //           on A.DataDictionaryHeadId equals B.DataDictionaryHeadId into AB
            //           from ABResut in AB.DefaultIfEmpty()
            //           select new
            //           {
            //               A=A,
            //               B= ABResut,
            //           };

            //var obj = linq.ToList();
            #endregion

            #region WebApi调用
            //Power power = new Power();
            //power.ParentId = "2";
            //power.PowerName = "2";
            //power.Url = "2";
            //power.IsPost = false;
            //string bodyJson = power.ToJson();
            //string result=WebApiCallerHr.HttpPost("http://127.0.0.1:27344/api/Test/AddPower", bodyJson);
            //string result2= WebApiCallerHc.HttpPost("http://127.0.0.1:27344/api/Test/AddPower", bodyJson);
            //string resutl3 = WebApiCallerHc.HttpGet("http://127.0.0.1:27344/api/Demo/Get");
            #endregion

            #region EF性能测试

            #region EF初始化优化

            ////EF默认的数据库初始化器(四种初始化方式，只有禁用初始化器这种方式比较靠谱)
            //System.Data.Entity.Database.SetInitializer<ModelContainer>(null);
            ////EF Migration作为数据库初始化器（发布部署应用程序的时候，当程序启动的时候它自动更新数据库，DbContext 第一次被使用的时候执行）
            //Database.SetInitializer(new MigrateDatabaseToLatestVersion<DbContext1, Configuration>());

            //首次使用EF的DbContext，内存中生成mapping views(耗时操作)
            //ModelContainer dbContext = new ModelContainer();
            //var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
            //var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
            //mappingCollection.GenerateViews(new List<EdmSchemaError>());

            #endregion


            #region 增加
            //ModelContainer modelContainer = new ModelContainer();

            //List<Test> testList = new List<Test>();
            //for (int i = 1000001; i <= 5000000; i++)
            //{
            //    Test testTemp = new Test();
            //    testTemp.TestId = i.ToString();
            //    testTemp.Name = "name" + i;
            //    testTemp.Value = "value" + i;
            //    testTemp.Description = "description" + i;
            //    testTemp.AddTime = DateTime.Now;
            //    testList.Add(testTemp);
            //}
            //DateTime beforDT = System.DateTime.Now;
            //foreach (var testIetm in testList)
            //{
            //    string sql = "insert into test (TestId,Name,Value,Description,AddTime) values('" + testIetm.TestId + "','" + testIetm.Name + "','" + testIetm.Value + "','" + testIetm.Description + "','" + testIetm.AddTime.Value.ToString("yyyy-MM-dd") + "')";
            //    //LogHelper.WriteDebugLog(typeof(string),sql);
            //    modelContainer.Database.ExecuteSqlCommand(sql);
            //}
            //DateTime afterDT = System.DateTime.Now;
            //TimeSpan ts = afterDT.Subtract(beforDT);
            //Console.WriteLine("增加400万条总共花费{0}ms.", ts.TotalMilliseconds);//42秒:ExecuteSqlCommand(10万条);（使用bin目录下exe）
            #endregion

            #region 查询
            //ModelContainer modelContainer = new ModelContainer();
            //DateTime beforDT = System.DateTime.Now;
            //var test = modelContainer.Tests.AsNoTracking().FirstOrDefault(a => a.TestId == "666");
            //DateTime afterDT = System.DateTime.Now;
            //TimeSpan ts = afterDT.Subtract(beforDT);
            //Console.WriteLine("查询消耗{0}ms.", ts.TotalMilliseconds);//155ms:10万；165ms:100万；165ms:500万（使用bin目录下exe）
            #endregion


            #endregion

            #region XmlHelper
            //XmlDocument doc = new XmlDocument();
            //doc.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Demo.xml"));
            //string nameAttribute = doc.GetAttributeText("/Demo/Jack","name");
            //doc.SetAttributeText("/Demo/Jack","name","改变后的name");
            //nameAttribute = doc.GetAttributeText("/Demo/Jack", "name");
            //string name = doc.GetInnerTextWithNamespace("/Resume/PersonalInformation/GeneralInformation/Name", "http://ns.test.com/resumes");
            //doc.SetInnerTextWithNamespace("/Resume/PersonalInformation/GeneralInformation/Name", "Jack", "http://ns.test.com/resumes");
            //string outXml = doc.OuterXml;
            //测试成功
            #endregion

            #region DbHelper

            //string sql = "select * " +
            //             "from UserInfo where LoginName=@LoginName";
            //IDbDataParameter[] sqlParameterArr = {
            //    new SqlParameter("@LoginName","admin")
            //};
            //var dt=DbHelper.GetDataTable(sql,sqlParameterArr);

            #endregion

            Console.ReadKey();
       }
    }
}
